package com.example.demo.controller;

import com.example.demo.Response;
import com.example.demo.dto.ClothDTO;
import com.example.demo.service.ClothService;
import com.example.demo.service.FileService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Set;

@RestController
public class ClothController {
    @Autowired
    private ClothService clothService;
    @Autowired
    private FileService fileService;

    @GetMapping("cloth/{userId}")
    public Response<Set<ClothDTO>> getClothByUserId(@PathVariable long userId) {

        return Response.newSuccess(clothService.getClothByUserId(userId));
    }

    @GetMapping("cloth/{userId}/category/{category}")
    public Response<Set<ClothDTO>> getClothByUserIdAndCategory(@PathVariable long userId, @PathVariable String category) {
        return Response.newSuccess(clothService.getClothByUserIdAndCategory(userId, category));
    }

    @GetMapping("cloth/{userId}/subcategory/{subcategory}")
    public Response<Set<ClothDTO>> getClothByUserIdAndSubcategory(@PathVariable long userId, @PathVariable String subcategory) {
        return Response.newSuccess(clothService.getClothByUserIdAndSubcategory(userId, subcategory));
    }

    @GetMapping("cloth/{userId}/userClothId/{userClothId}")
    public Response<ClothDTO> getClothByUserIdAndUserClothId(@PathVariable long userId, @PathVariable int userClothId) {
        return Response.newSuccess(clothService.getClothByUserIdAndUserClothId(userId, userClothId));
    }

    @PostMapping("/cloth/{userId}")
    public Response<Long> addClothByUserId(
            @PathVariable long userId,
            @RequestParam(value = "imageFile", required = false) MultipartFile imageFile,
            @RequestParam("clothDTO") String clothDataJson) {
        try {
            // 将 JSON 转换为 ClothDTO 对象
            ObjectMapper objectMapper = new ObjectMapper();
            ClothDTO clothDTO = objectMapper.readValue(clothDataJson, ClothDTO.class);
            if (imageFile != null) {
                String imageName = fileService.storeFile(imageFile);
                clothDTO.setImageName(imageName);
            }
            // 添加衣物并返回衣物 ID
            long clothId = clothService.addClothByUserId(clothDTO, userId);

            // 返回成功响应
            return Response.newSuccess(clothId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("cloth/{userId}/{userClothId}")
    public void deleteClothByUserIdAndUserClothId(@PathVariable long userId, @PathVariable int userClothId) {
        clothService.deleteClothByUserIdAndUserClothId(userId, userClothId);
    }

    @PutMapping("cloth/{userId}/{userClothId}")
    public Response<ClothDTO> updateClothByUserIdAndUserClothId(@PathVariable long userId, @PathVariable int userClothId, @RequestParam(required = false) String clothName, @RequestParam(required = false) String imageUrl, @RequestParam(required = false) String category, @RequestParam(required = false) String subcategory) {
        return Response.newSuccess(clothService.updateClothByUserIdAndUserClothId(userId, userClothId, clothName, imageUrl, category, subcategory));
    }

}
